/*
 * Email Notify
 * Author: Antonio Nastasi
 * Email: sifcenter (at) gmail.it
 * Version: 0.2
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "plasma-emailnotify.h"
#include <QPainter>
#include <QFontMetrics>
#include <QSizeF> 
#include <QString>
#include <plasma/svg.h>
#include <plasma/theme.h>
#include <KStandardDirs>

 
EmailNotify::EmailNotify(QObject *parent, const QVariantList &args)
    : Plasma::Applet(parent, args),
    m_svg("", this),
    m_icon("emailnotify")
{   	   
    setHasConfigurationInterface(true);
    setDrawStandardBackground(true);
    resize(210, 210);    
}
 
EmailNotify::~EmailNotify(){
    if (failedToLaunch()) {
	qDebug() << "failed to launch";
    } else {
	emailChecker.exit();	
	delete media;
	delete config;
	delete timer;
	delete generalGroup;
    }
   
}

/* configure dialog: login,password
 * interval,sound			*/
void EmailNotify::showConfigurationInterface()
{   
    configDialog.show();
}

void EmailNotify::playSound()
{    
    qDebug() << "PLAYED SOUND";
    if(!sound_file.compare("(Nothing)") || sound_file.isEmpty())
	return;
    QString file(KGlobal::dirs()->locate("sound",sound_file));    
    if(file.isEmpty())
	return;    
    media->setCurrentSource(file);
    media->play();
}


void EmailNotify::checkConfig()
{
    bool ok;
    config->reparseConfiguration ();
    server = generalGroup->readEntry( "Server", QString() );
    port = generalGroup->readEntry( "Port", QString().toInt(&ok,10));
    protocol = generalGroup->readEntry( "Protocol", QString());
    ssl = generalGroup->readEntry( "SSL", QString().toInt(&ok,10));   
    login = generalGroup->readEntry( "Login", QString() );
    password = QByteArray::fromBase64(generalGroup->readEntry( "Password", QString()).toUtf8());
    interval = (generalGroup->readEntry("Interval",QString())).toInt(&ok,10);
    sound_file = generalGroup->readEntry( "Sound", QString() );   
}

 
void EmailNotify::init()
{
    m_svg.setContentType(Plasma::Svg::SingleImage);   
    config = new KConfig("emailnotifyrc");
    generalGroup = new KConfigGroup(config,"General");  
       
    media = Phonon::createPlayer(Phonon::MusicCategory);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(checkEmails()));    
    connect(&emailChecker,SIGNAL(updatedText(QString)),this,SLOT(setText(QString)));
    connect(&emailChecker,SIGNAL(emailReceived(int)),this,SLOT(playSound()));
    text = "Email Notify";
    if (m_icon.isNull()) {
        setFailedToLaunch(true, "Loading error!");
    }
    /* Start Timer for check emails */
    timer->start(1000);
    /* Start thread */
    emailChecker.start();
} 

void EmailNotify::checkEmails()
{    
    checkConfig();
    emailChecker.setInfo(server,port,protocol,ssl,login,password);
    qDebug() << "checkEmails";
    emailChecker.checkEmails();
    if(interval !=0)
	timer->setInterval(interval*1000);
}

void EmailNotify::setText(QString text_)
{
    text = text_;
    update();
}
 
void EmailNotify::paintInterface(QPainter *p,
        const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
{
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->setRenderHint(QPainter::Antialiasing);   
    m_svg.resize((int)contentsRect.width(), (int)contentsRect.height());
    m_svg.paint(p, (int)contentsRect.left(), (int)contentsRect.top());    
    p->drawPixmap(7, 0, m_icon.pixmap((int)contentsRect.width(),(int)contentsRect.width()-14));
    p->save();   
    p->setPen(Qt::white);
    p->drawText(contentsRect, Qt::AlignBottom | Qt::AlignHCenter, text);
   // p->drawText(QPoint(115,128),text);
    p->restore();
}
 
#include "plasma-emailnotify.moc"
